﻿Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Seuss.EnterpriseLibrary.DataAccess
Imports Seuss.EnterpriseLibrary.BusinessLogic
Imports System.Web
Imports System.Data
Imports System.IO

Partial Class Consulta_DO_CursoAsig_RecProp

    Inherits System.Web.UI.Page
    Dim cDocCodigo As String
    Dim nDocLin As String
    Dim nCurGruCodigo As String
    Dim valuepath As String
    Dim compartido As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("nCurGruCodigo") Is Nothing Or Request.QueryString("nCurGruCodigo") Is String.Empty Or _
            Request.QueryString("cDocCodigo") Is Nothing Or Request.QueryString("cDocCodigo") Is String.Empty Or _
             Request.QueryString("nDocLin") Is Nothing Or Request.QueryString("nDocLin") Is String.Empty Or _
                Request.QueryString("value") Is Nothing Or Request.QueryString("value") Is String.Empty Then
            Response.Redirect("DO_CursoAsignado.aspx")
        Else
            Session("nCurGruCodigo") = Request.QueryString("nCurGruCodigo")
            Session("cDocCodigo") = Request.QueryString("cDocCodigo")
        End If
        Response.Expires = -1
        Response.AddHeader("Pragma", "no-cache")
        Response.AddHeader("cache-control", "private")
        Response.CacheControl = "no-cache"
        cDocCodigo = Request.QueryString("cDocCodigo")
        nDocLin = Request.QueryString("nDocLin")
        nCurGruCodigo = Request.QueryString("nCurGruCodigo")
        valuepath = Request.QueryString("value")
        lnkRegresar_3.HRef = "./DO_CursoAsig_Rec.aspx?nCurGruCodigo=" + nCurGruCodigo
        lnkRegresar_4.HRef = "./DO_CursoAsig_Rec.aspx?nCurGruCodigo=" + nCurGruCodigo
        tablacomp.Visible = True
        lblFaltaNombre.Text = ""
        If Not Page.IsPostBack Then

            Dim ldrRecurso As New List(Of IDataReader)
            Using ObjPool As New PoolDbCommand()
                Dim Objgrupo As New DAL_Kiosco(ObjPool.Database)
                ObjPool.AddDbCommand(Objgrupo.Get_DocLink_By_cDocCodigo_nDocLinNum(cDocCodigo, nDocLin), PoolDbCommand.PoolDbCommandResultType.Query)
                ObjPool.Pooling(Nothing, ldrRecurso, True)
            End Using
            Dim Pat As String = ConfigurationManager.AppSettings("DiscoVirtual")
            If ldrRecurso(0).Read Then
                lblrecurso.Text = ldrRecurso(0).Item("cDocLinDescripcion")
                lblusuario.Text = ldrRecurso(0).Item("ALIAS")
                If Not (ldrRecurso(0).Item("nCarCodigo").ToString = "1" And ldrRecurso(0).Item("cpercodigo").ToString <> IdentityUser.UserCode) Then
                    If ldrRecurso(0).Item("nDocLinTipo").ToString = "2" Then
                        lbldescarga.Text = "<a href=""" + Pat + ldrRecurso(0).Item("cDocLinUrl").ToString + """ target=_blank rel=""shadowbox;width=800;height=600"">Descargar</a>"
                    ElseIf ldrRecurso(0).Item("nDocLinTipo") <> 1 Then
                        lbldescarga.Text = "<a href=""" + ldrRecurso(0).Item("cDocLinUrl").ToString + """ target=_blank rel=""shadowbox;width=800;height=600"">Ver</a>"
                    End If

                End If
                If ldrRecurso(0).Item("nDocLinTipo").ToString = "1" Then
                    lbltipo.Text = "Carpeta"
                ElseIf ldrRecurso(0).Item("nDocLinTipo").ToString = "2" Then
                    lbltipo.Text = "Archivo"
                Else
                    lbltipo.Text = "Enlace"
                End If

                If ldrRecurso(0).Item("nDocLinGrupo").ToString = "0" Then
                    PanelSubCarpeta.Visible = True
                Else
                    PanelSubCarpeta.Visible = False
                End If

                If ldrRecurso(0).Item("Estado") = "Compartido" Then
                    chkestado.Checked = True
                    compartido = True
                Else
                    chkestado.Checked = False
                    compartido = False
                End If
                If ldrRecurso(0).Item("nDocLinTipo") <> 1 Then
                    tablacomp.Visible = False
                End If
            End If
            Using Seguridad As New BLL_Administracion
                Seguridad.Set_Transaccion_Insert(Nothing, 100401, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Consulta Pagina Movil", "/Pagina|" + My.Request.Url.Segments(My.Request.Url.Segments.Length - 1).ToString))
            End Using
        End If
    End Sub

    Protected Sub btnaddarchivo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnaddarchivo.Click

        Try

            Dim tipo As String = Right(UFArchivo.FileName, 3)
            Dim tipo2 As String = Right(UFArchivo.FileName, 4)
            If UFArchivo.FileName.Length > 3 Then
                If tipo.ToString.ToUpper = "ZIP" Or tipo.ToString.ToUpper = "RAR" Then

                    Dim vTamano As Double
                    vTamano = ((UFArchivo.PostedFile.ContentLength / 1024) / 1024) ' Bytes/Kb/Mb

                    If tipo.ToString.ToUpper = "SWF" Or tipo.ToString.ToUpper = "AVI" Then
                        If vTamano > 40 Then
                            lblmensaje.Text = "<span style='color:red'>Para los arhvios tipo .SWF, .AVi su tamaño no debe exceder lo 40 MB.</span>"
                            Exit Sub
                        End If
                    Else
                        If vTamano > 5 Then
                            lblmensaje.Text = "<span style='color:red'>Para los arhvios tipo .RAR, .ZIP o .PDF su tamaño no debe exceder lo 5 MB. </span>"
                            Exit Sub
                        End If
                    End If

                    Dim Nodo As String = valuepath
                    Dim c As String() = Nodo.Split(New Char() {"/"c})
                    Dim s As String = c(1)
                    Dim a As String() = s.Split(New Char() {"|"c})
                    Dim snodo As String = a(0).Trim
                    Dim sValue As String = a(1).Trim()
                    Dim Direccion As String
                    Dim ruta As String = ConfigurationManager.AppSettings("Dir")

                    Dim RutaCarp As String = ""
                    Dim RutaCarp2 As String = ""
                    For i = 0 To CInt(c.Length) - 1
                        s = c(i)
                        a = s.Split(New Char() {"|"c})
                        If i > 0 Then
                            snodo = a(0).Trim
                            RutaCarp = RutaCarp + "\" + snodo
                            RutaCarp2 = RutaCarp2 + "/" + snodo
                            sValue = a(1).Trim()
                        End If
                    Next

                    'ruta = ruta + "\" + lblncurgrucodigo.Text + "\" + snodo + "\" + IdentityUser.UserCode + "\"
                    ruta = ruta + "\" + nCurGruCodigo + "" + RutaCarp + "\" + IdentityUser.UserCode + "\"

                    CreateDirectory(ruta)

                    'Direccion = lblncurgrucodigo.Text + "/" + snodo + "/" + IdentityUser.UserCode + "/" + Path.GetFileName(UFArchivo.PostedFile.FileName)
                    Direccion = nCurGruCodigo + "" + RutaCarp2 + "/" + IdentityUser.UserCode + "/" + Path.GetFileName(UFArchivo.PostedFile.FileName)

                    ruta = ruta + Path.GetFileName(UFArchivo.PostedFile.FileName)
                    UFArchivo.PostedFile.SaveAs(ruta)

                    Using ObjPool As New PoolDbCommand()
                        Dim Objgrupo As New DAL_Kiosco(ObjPool.Database)
                        ObjPool.AddDbCommand(Objgrupo.Ins_Archivo(cDocCodigo, IdentityUser.UserCode, 2, 2, sValue, Direccion, txtDescripcion.Text), PoolDbCommand.PoolDbCommandResultType.NonQuery)
                        If ObjPool.Pooling() Then
                            Using Seguridad As New BLL_Administracion
                                Seguridad.Set_Transaccion_Insert(Nothing, 100402, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Registrar Recurso", "/cDocCodigo|" + cDocCodigo + "/Descripcion|" + txtDescripcion.Text + "/nCurGruCodigo|" + nCurGruCodigo))
                            End Using
                        End If
                    End Using
                    lblmensaje.Text = "<span style='color:blue'>Se ha guardado correctamente</span>"
                Else
                    lblmensaje.Text = "<span style='color:red'>Tipo de archivo incorrecto</span>"
                End If
                txtDescripcion.Text = ""
            Else
                lblmensaje.Text = "<span style='color:red'>Seleccione Archivo</span>"
            End If
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub

    Public Function CreateDirectory(ByVal FullPath As String) As Boolean

        Dim objDI As New DirectoryInfo(FullPath)

        Try
            objDI.Create()
            Return True
        Catch
            Return False
        End Try
    End Function

    Protected Sub btnAddenlace_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddenlace.Click
        Try
            If txturl.Text <> "" Then


                Dim Nodo As String = valuepath
                Dim c As String() = Nodo.Split(New Char() {"/"c})
                Dim s As String = c(1)
                Dim a As String() = s.Split(New Char() {"|"c})
                Dim snodo As String = a(0).Trim
                Dim sValue As String = a(1).Trim()

                Using ObjPool As New PoolDbCommand()
                    Dim Objgrupo As New DAL_Kiosco(ObjPool.Database)
                    ObjPool.AddDbCommand(Objgrupo.Ins_Archivo(cDocCodigo, IdentityUser.UserCode, 2, 3, sValue, txturl.Text, txtDescripcion.Text), PoolDbCommand.PoolDbCommandResultType.NonQuery)
                    If ObjPool.Pooling() Then
                        Using Seguridad As New BLL_Administracion
                            Seguridad.Set_Transaccion_Insert(Nothing, 100402, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Registrar Enlace", "/cDocCodigo|" + cDocCodigo + "/Descripcion|" + txtDescripcion.Text + "/nCurGruCodigo|" + nCurGruCodigo))
                        End Using
                    End If
                End Using
            Else
                lblErrorUrl.Text = "Ingrese URL"
            End If
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub

    Protected Sub DeleteRecurso(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnDelRecurso.Click
        Dim Nodo As String
        Dim c As String()
        Dim s As String
        Dim a As String()
        Dim snodo As String
        Dim sValue As String = ""

        Nodo = valuepath
        c = Nodo.Split(New Char() {"/"c})

        For i = 0 To c.Length - 1
            s = c(i)
            a = s.Split(New Char() {"|"c})
            If i > 0 Then
                snodo = a(0).Trim
                sValue = a(1).Trim()
            End If
        Next

        Using ObjPool As New PoolDbCommand()
            Dim Objgrupo As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(Objgrupo.Del_Recurso_By_cDocCodigo_nItem(cDocCodigo, sValue), PoolDbCommand.PoolDbCommandResultType.NonQuery)
            If ObjPool.Pooling() Then
                Using Seguridad As New BLL_Administracion
                    Seguridad.Set_Transaccion_Insert(Nothing, 100404, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Actualiza Recurso", "/cDocCodigo|" + cDocCodigo.ToString + "/nDocLinNum|" + sValue.ToString))
                End Using
            End If
        End Using

        Response.Redirect("DO_CursoAsig_Rec.aspx")
    End Sub

    Protected Sub btnUpdEstado_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdEstado.Click
        Dim Nodo As String
        Dim c As String()
        Dim s As String
        Dim a As String()
        Dim snodo As String
        Dim sValue As String = ""

        Nodo = valuepath
        c = Nodo.Split(New Char() {"/"c})

        For i = 0 To c.Length - 1
            s = c(i)
            a = s.Split(New Char() {"|"c})
            If i > 0 Then
                snodo = a(0).Trim
                sValue = a(1).Trim()
            End If
        Next
        Using ObjPool As New PoolDbCommand()
            Dim Objgrupo As New DAL_Kiosco(ObjPool.Database)
            ObjPool.AddDbCommand(Objgrupo.Upd_Estado_Recurso_By_cDocCodigo_nDocLinNum(cDocCodigo, sValue, IIf(chkestado.Checked = True, 2, 1)), PoolDbCommand.PoolDbCommandResultType.NonQuery)
            If ObjPool.Pooling() Then
                Using Seguridad As New BLL_Administracion
                    Seguridad.Set_Transaccion_Insert(Nothing, 100403, IdentityUser.UserCode, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("Actualiza Recurso", "/cDocCodigo|" + cDocCodigo.ToString + "/nDocLinNum|" + sValue.ToString + "/Estado|" + chkestado.Checked.ToString))
                End Using
            End If
        End Using
        Response.Redirect("DO_CursoAsig_RecProp.aspx?cDocCodigo=" + cDocCodigo + "&nDocLin=" + nDocLin + "&nCurGruCodigo=" + nCurGruCodigo + "&value=" + valuepath)

    End Sub

    Private Sub btnAgregaSub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregaSub.Click

        If txtNombreCarpeta.Text <> "" Then
            Try
                Dim Nodo As String = valuepath
                Dim c As String() = Nodo.Split(New Char() {"/"c})
                Dim s As String = c(1)
                Dim a As String() = s.Split(New Char() {"|"c})
                Dim snodo As String = a(0).Trim
                Dim sValue As String = a(1).Trim()
                Dim ruta As String = ConfigurationManager.AppSettings("Dir")

                ruta = ruta + "\" + nCurGruCodigo + "\" + snodo + "\" + txtNombreCarpeta.Text
                CreateDirectory(ruta)

                Using ObjPool As New PoolDbCommand()
                    Dim Objgrupo As New Dal_Kiosco(ObjPool.Database)
                    ObjPool.AddDbCommand(Objgrupo.Ins_Archivo(cDocCodigo, IdentityUser.UserCode, 2, 1, sValue, txtNombreCarpeta.Text, txtNombreCarpeta.Text), PoolDbCommand.PoolDbCommandResultType.NonQuery)
                    ObjPool.Pooling()
                End Using
                txtNombreCarpeta.Text = ""
                Response.Redirect("DO_CursoAsig_Rec.aspx")
            Catch ex As Exception

            End Try
        Else
            lblFaltaNombre.Text = "* Ingrese nombre de carpeta"
        End If
    End Sub
End Class